﻿namespace MefContrib.Models.Provider
{
    using System.ComponentModel.Composition.Primitives;
    using System.Globalization;
    using System.Reflection;
    using MefContrib.Models.Provider.Validation;

    /// <summary>
    /// Defines an export required by a <see cref="ProviderComposablePart"/> object.
    /// </summary>
    public class ProviderExportDefinition : ExportDefinition
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="ProviderExportDefinition"/> class, 
        /// using the specified values to define the export.
        /// </summary>
        /// <param name="member">The member that the exort is associated with.</param>
        /// <param name="exportDescription"></param>
        public ProviderExportDefinition(MemberInfo member, ExportDescription exportDescription)
            : base(
            CompositionServices.GetContractNameFromExportDescription(member, exportDescription),
            CompositionServices.GetMetadataFromExportDescription(member, exportDescription))
        {
            member
                .Require("member")
                .NotNull();

            exportDescription
                .Require("exportDescription")
                .NotNull();
            
            Member = member;
        }

        /// <summary>
        /// Gets the member that the export associated with.
        /// </summary>
        /// <value>A <see cref="MemberInfo"/> object.</value>
        public MemberInfo Member { get; private set; }

        public override string ToString()
        {
            return string.Format(CultureInfo.CurrentCulture,
                                 "{0} (ContractName=\"{1}\")", Member.Name, ContractName);
        }
    }
}